package kltn.data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.annotation.SuppressLint;
import android.content.res.AssetManager;
import android.util.Log;

@SuppressLint("SdCardPath")
public class CopyDB {
	
	
	public void update(InputStream stream){
		File db = new File("/data/data/kltn.audioguidetravel/databases/STGDataBase.sqlite");
			InputStream inputStream = null;
			FileOutputStream fileOutputStream = null;

			try {
				inputStream = stream;
				byte[] buffer = new byte[inputStream.available()];
				Log.d("test", inputStream.available()+" byte copy "+buffer.length);
				inputStream.read(buffer);

				fileOutputStream = new FileOutputStream(db);
				fileOutputStream.write(buffer);
				Log.d("test", "Update thành công!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					//inputStream.close();
					fileOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
	}
	
	public void copy(AssetManager assetManager) {
		
		File f=new File("/data/data/kltn.audioguidetravel/databases");
		if(!f.exists()){
				f.mkdir();
		}
		File db = new File("/data/data/kltn.audioguidetravel/databases/STGDataBase.sqlite");
		if (!db.exists()) {
			InputStream inputStream = null;
			FileOutputStream fileOutputStream = null;

			try {
				inputStream = assetManager.open("STGDataBase.sqlite");
				byte[] buffer = new byte[inputStream.available()];
				Log.d("test", inputStream.available()+" byte "+buffer.length);
				inputStream.read(buffer);

				fileOutputStream = new FileOutputStream(db);
				fileOutputStream.write(buffer);

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					inputStream.close();
					fileOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}
}
